package com.sjz.myfrist;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 *  防止重复点击
 *  以下示例1s内不能重复点击按钮
 */
public class RepeatClick extends AppCompatActivity {
    private static long lastClickTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.repeat_click);
        Button button = (Button) findViewById(R.id.repeat_button);
        TextView textView = (TextView) findViewById(R.id.repeat_text);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isFastDoubleShake()){
                    textView.setText("点击成功");
                }else{
                    textView.setText("点击太频繁，稍后再试");                }
            }
        });
    }

    //  防止短时间重复摇动
    public static boolean isFastDoubleShake() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if ( 0 < timeD && timeD < 1000) {
            return false;
        }
        lastClickTime = time;
        return true;
    }
}